package weaver.aiyh_pcn.workflow_to_customer.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author EBU7-dev1-ayh
 * @create 2021/8/13 0013 17:06
 * SQLwhere 构建SQL的条件，并且以prepareStatement参数的形式返回
 */


public class PrepSQLWhere {
	private final StringBuilder pstSqlWhere;
	private final List<Object> args;

	public PrepSQLWhere(PrepWhere where) {
		this.pstSqlWhere = where.pstSqlWhere;
		this.args = where.args;
	}

	public StringBuilder getPstSqlWhere() {
		return pstSqlWhere;
	}

	public List<Object> getArgs() {
		return args;
	}

	public static PrepWhere creatPrepWhere() {
		return new PrepWhere();
	}

	public static class PrepWhere {
		private final StringBuilder pstSqlWhere;
		private final PrepWhereOperator whereOperator;
		private final List<Object> args;

		public PrepWhere() {
			this.pstSqlWhere = new StringBuilder(" 1 = 1 ");
			this.whereOperator = new PrepWhereOperator(this);
			args = new ArrayList<>();
		}

		public PrepWhereOperator and(String key) {
			this.pstSqlWhere.append(" and ");
			this.pstSqlWhere.append(key);
			return whereOperator;
		}

		public PrepWhereOperator or(String key) {
			this.pstSqlWhere.append(" or ");
			this.pstSqlWhere.append(key);
			return whereOperator;
		}


		public PrepSQLWhere create() {
			return new PrepSQLWhere(this);
		}
	}

	public static class PrepWhereOperator {
		private final PrepWhere where;

		public PrepWhereOperator(PrepWhere where) {
			this.where = where;
		}

		public PrepWhere isNull(String key) {
			where.pstSqlWhere.append(" is null ");
			return where;
		}

		public PrepWhere isNotNUll(String key) {
			where.pstSqlWhere.append(" is not null ");
			where.pstSqlWhere.append(key);
			return where;
		}

		public PrepWhere equal(Object value) {
			where.pstSqlWhere.append(" = ? ");
			where.args.add(value);
			return where;
		}

		public PrepWhere noEqual(Object value) {
			where.pstSqlWhere.append(" <> ? ");
			where.args.add(value);
			return where;
		}

		public PrepWhere equalSql(Object value) {
			where.pstSqlWhere.append(" in ( ");
			where.pstSqlWhere.append(value);
			where.pstSqlWhere.append(" ) ");
			return where;
		}

		public PrepWhere noEqualSql(Object value) {
			where.pstSqlWhere.append(" not in ( ");
			where.pstSqlWhere.append(value);
			where.pstSqlWhere.append(" ) ");
			return where;
		}

		public PrepWhere in(List<Object> list) {
			where.pstSqlWhere.append(" in ( ");
			list.forEach(item -> where.pstSqlWhere.append(" ? "));
			where.pstSqlWhere.append(" ) ");
			where.args.addAll(list);
			return where;
		}

		public PrepWhere in(Object ...list) {
			where.pstSqlWhere.append(" in ( ");
			for(Object obj : list){
				where.pstSqlWhere.append(" ? ");
				where.args.add(obj);
			}
			where.pstSqlWhere.append(" ) ");
			return where;
		}

		public PrepWhere notIn(List<Object> list) {
			where.pstSqlWhere.append(" not in ( ");
			list.forEach(item -> where.pstSqlWhere.append(" ? "));
			where.pstSqlWhere.append(" ) ");
			where.args.addAll(list);
			return where;
		}

		public PrepWhere notIn(Object ...list) {
			where.pstSqlWhere.append(" not in ( ");
			for(Object obj : list){
				where.pstSqlWhere.append(" ? ");
				where.args.add(obj);
			}
			where.pstSqlWhere.append(" ) ");;
			return where;
		}

		public PrepWhere like(Object value) {
			where.pstSqlWhere.append(" like ? ");
			where.args.add(value);
			return where;
		}

		public PrepWhere greaterOrEqual(Object value) {
			where.pstSqlWhere.append(" >= ? ");
			where.args.add(value);
			return where;
		}

		public PrepWhere lessOrEqual(Object value) {
			where.pstSqlWhere.append(" <= ? ");
			where.args.add(value);
			return where;
		}

		public PrepWhere BetweenAnd(Object value1, Object value2) {
			where.pstSqlWhere.append(" between ? ");
			where.args.add(value1);
			where.pstSqlWhere.append(" and ? ");
			where.args.add(value2);
			return where;
		}
	}

}
